<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

//基本路由
//详见：手册【Http层】【14、HTTP层 —— 路由】
/**
 Http请求类型：[get | post|put|delete...]
 @param [string] $[name] [URL地址]
 @param [closure闭包] [PHP的处理程序]
 http://www.laravel5.com/test
  */
Route::get('/test', function(){
	// echo __METHOD__;
	// dd(App\User::class);
	// dd(config('app'));
	// dd(config('auth'));
	// dd(captcha());
	echo date('Y-m-d H:i:s',time());
	return "这里是基本路由。。。";
});
Route::get('/test2', function(){
	// dd(captcha());
	return view('test2');
});
Route::get('/session', function(){
	session_start();
	echo "<pre>";
	print_r($_SESSION);
	echo "</pre>";
});

//带必选参数的路由
/* 
 http://www.laravel5.com/testp/100
 */
Route::get('/testp/{id}', function($id2){
	return "这里是带必选参数的路由。。。参数是$id2";
});

//带可选参数的路由
// http://www.laravel5.com/testop
// http://www.laravel5.com/testop/123
Route::get('/testop/{id?}', function($id=0){
	return "这里是带可选参数的路由。。。参数是".$id;
});


//参数的正则约束（单参数）
// http://www.laravel5.com/testrule/300
// http://www.laravel5.com/testrule/abc		//报错，参数不符合约束（约束是'\d+'即必须是数字）
Route::get('/testrule/{id}', function($id){
	return "这里是参数的正则约束。。。";
})->where('id', '\d+');

//参数的正则约束（多参数）
// http://www.laravel5.com/testrules/xiaoming/20
Route::get('/testrules/{name}/{age}', function($n, $a){
	return "多参数正则约束。。。name=$n ; age=$a";
})->where(['name' => '\w+', 'age' => '\d+']);


//路由到控制器方法
Route::get('/test/fn', 'TestController@lst');
Route::get('/user/lst', 'Test\UserController@lst');

//中间件
Route::get('/login', function(){
	session(['uid' => 100]);
	return "这里是login登录页面。。。";
});
Route::get('setting', function(){
	return "这里是setting路由。。。uid=".session('uid');
})->middleware('login');


//http 请求
// Route::get('/user/add', 'Test\UserController@add');
// Route::post('/user/add', 'Test\UserController@add');
//上面两行可以优化为如下：
Route::match(['get', 'post'], '/user/add', 'Test\UserController@add');


//Http响应
	//1。基本响应
Route::get('/response', function(){
	// return response('hello world', 200)->header("content-type", "text/html;charset=utf-8");
	// return response('hello world', 304)->header("content-type", "text/html;charset=utf-8");
	// return response('')->header('x-http-noe', 'one')->header("content-type", "text/html;charset=utf-8");
	return response('hello world', 200)->header('x-http-noe', 'one')->header("content-type", "text/html;charset=utf-8");
});

	//2。设置cookie
Route::get('/response/cookie', function(){
	return response('')->withCookie('name', 'xiaoming', 10);
});
	//2-2。获取cookie
Route::get('/cookie', function(){
	dd(Cookie::get('name')); //cookie门面
});

	//3。ajax
Route::get('/ajax', function(){
	return view('ajax');
});
Route::get('/response/ajax', function(){
	return response()->json(['name' => "xiaoqiang", 'age' => 20]);
	// return "333";
});
Route::get('/response/ajax2', 'TestController@ajax2');

//〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
//数据库操作
//1、插入操作
Route::get('/db/insert', function(){
	$re = DB::insert("insert into it_user values (null, 'xiaoming', '123456')");
	dd($re);
});

//2、查询操作
Route::get('/db/select', function(){
	$arr_data = DB::select("select * from it_user where id < 10");
	dd($arr_data);
});

//3、更新操作
Route::get('/db/update', function(){
	$re = DB::update("update it_user set name='小美' where id = 2");
	dd($re);//返回1，表示受影响行数；操作失败，用($re === false)来判断
});

//4、参数绑定
Route::get('/db/selectp', function(){
	$arr = DB::select('select * from it_user where id = :id', ['id' => 1]);	//关联数组 -- 使用字符串占位符
	// $arr = DB::select('select * from it_user where id = ?', [2]);		//索引数组 -- 使用？占位符
	dd($arr);
});

//5、删除操作
Route::get('/db/delete', function(){
	$re = DB::delete("delete from it_user where id = 3");
	dd($re);//返回1，表示受影响行数；操作失败，用($re === false)来判断
});

//〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
//构建器的使用（构建器，就是组建一个SQL语句）
//1、获取所有记录
Route::get('/db/get', function(){
	// dd(DB::table('user'));
	$arr = DB::table('user')->get();
	dd($arr);
});

//2、获取单条记录
Route::get('/db/first', function(){
	$info = DB::table('user')->first();
	dd($info);
});

//3、获取一列数据
Route::get('/db/pluck', function(){
	// $data = DB::table('user')->pluck('name');	//返回索引数据
	$data = DB::table('user')->pluck('name', 'id');	//返回关联数据
	dd($data);
});

//4、where子句
Route::get('/db/where', function(){
	// dd(DB::table('user')->where('id', '=', '4'));
	dd(DB::table('user')->where('id', '=', '4')->get());
});

//5、insert操作
Route::get('/db/insert2', function(){
	$re = DB::table('user')->insert(['name' => '小红', 'password' => '123456']);
	dd($re);
});

//6、insertid操作
Route::get('/db/insertid', function(){
	//插入一条记录；返回插入记录的ID
	$id = DB::table('user')->insertGetId(['name' => '小张', 'password' => '123456']);
	dd($id);
});

//7、update操作
Route::get('/db/update2', function(){
	$rows = DB::table('user')->where('id', '5')->update(['name' => 'Tom']);
	dd($rows);
});

//7、delete操作
Route::get('/db/delete2', function(){
	$rows = DB::table('user')->where('id', '>=', '7')->delete();
	//delete方法，返回0，表示没有删除对应的记录；返回false，表示删除失败。
	dd($rows);
});

//〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
//Eloquent ORM的CURD
//列表路由
Route::get('/orm/lst', 'ORM\UserController@lst');
//添加路由
Route::match(['get', 'post'], '/orm/add', 'ORM\UserController@add');
//修改路由
Route::match(['get', 'post'], '/orm/edt/{id}', 'ORM\UserController@edt')
	->where('id', '\d+');
//删除的路由
Route::get('/orm/del/{id}', 'ORM\UserController@del')
	->where('id', '\d+');

//登录模块
Route::match(['get', 'post'], '/admin/login', 'Admin\ManagerController@login')
	->name('login');

//css引用测试
Route::get('/css/fn', 'Css\CssController@fn');

//后台模块
//使用group是为了防止翻墙访问
Route::group(['middleware' => 'auth:admin'], function(){
	Route::get('/admin/index', 'Admin\IndexController@index');
	Route::get('/admin/welcome', 'Admin\IndexController@welcome');
	Route::get('/admin/logout', 'Admin\ManagerController@logout');
	
	Route::get('/admin/manager/lst', 'Admin\ManagerController@lst');
	Route::match(['get', 'post'], '/admin/manager/add', 'Admin\ManagerController@add');
	Route::post('/admin/manager/del', 'Admin\ManagerController@del');
});

//〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓〓
//Eloquent ORM的关联关系
Route::get('/orm/relation/{mode}', 'ORM\UserController@relation');






















